//
//  YXYGroupNextViewController.m
//  Topit
//
//  Created by dllo on 15/12/11.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYGroupNextViewController.h"
#import "YXYGroupNextHeadModel.h"
#import "YXYGroupNextViewModel.h"
#import "YXYGroupNextTableViewCell.h"
#import "YXYSelectionViewController.h"
#import "MJRefresh.h"
@interface YXYGroupNextViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, copy) NSString *url;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSMutableArray *viewArray;
@property (nonatomic, assign) int number;
@end

@implementation YXYGroupNextViewController

- (void)dealloc {
    [_viewArray retain];
    [_dataSource release];
    [_url release];
    [super dealloc];
}

- (instancetype)initWithURL:(NSString *)URL
{
    self = [super init];
    if (self) {
        self.url = URL;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"小组主页";
    [self getDate];
    [self setUptableView];
    [self getFootRefresh];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.translucent = NO;
    [self.tableView reloadData];
}

#pragma mark - getDate 
-(void)getDate {
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.viewArray = [NSMutableArray arrayWithCapacity:0];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.mode = MBProgressHUDModeIndeterminate;
    [YXYNetWorkTool getWithUrl:self.url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
   
        YXYGroupNextHeadModel *groupNextHeadModel = [[YXYGroupNextHeadModel alloc] initWithDictionary:result[@"info"][@"sbj"]];
        [self.dataSource addObject:groupNextHeadModel];
        [groupNextHeadModel release];
        for (NSDictionary *dic in result[@"item"]) {
            YXYGroupNextViewModel *groupNextViewModel = [[YXYGroupNextViewModel alloc] initWithDictionary:dic];
            if (groupNextViewModel.items.count == 0) {
                [_viewArray addObject:groupNextViewModel];
                [groupNextViewModel release];
            }
        }
        [hud hide:YES afterDelay:1];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    } fail:^(NSError *error) {
        [hud hide:YES];
       [ProgressHUD showError:@"请检查网络"]; 
    }];
}

#pragma mark - 底部刷新
- (void)getFootRefresh {
    
    [self.tableView addFooterWithCallback:^{
        self.number += 30;
        NSString *num = [NSString stringWithFormat:@"%d", _number];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:num, @"offset", nil];
        [YXYNetWorkTool getWithUrl:self.url parameter:dic httpHeader:dic responseType:ResponseTypeJSON success:^(id result) {
            for (NSDictionary *dic in result[@"item"]) {
                YXYGroupNextViewModel *groupNextViewModel = [[YXYGroupNextViewModel alloc] initWithDictionary:dic];
                if (groupNextViewModel.items.count == 0) {
                    
                    [_viewArray addObject:groupNextViewModel];
                    [groupNextViewModel release];
                }
            }
            
            [_tableView reloadData];
            
            [_tableView footerEndRefreshing];
            
        } fail:^(NSError *error) {
            
        }];
        
    }];
    
}


#pragma mark - tableView
- (void)setUptableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[YXYGroupNextTableViewCell class] forCellReuseIdentifier:@"YXYGroupNextTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
    [_tableView release];
}

#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"NightMode"];

    
    YXYGroupNextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXYGroupNextTableViewCellIdentifier"];
    YXYGroupNextViewModel *groupNextViewModel = self.viewArray[indexPath.row];
    [cell setGroupNextViewModel:groupNextViewModel];
    if ([passWord isEqualToString:@"Night"]) {
        cell.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:34 / 255.0 blue:34 / 255.0 alpha:1];
    }else {
        cell.backgroundColor = [UIColor whiteColor];
        
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YXYGroupNextHeadModel *groupNextHeadModel = [self.dataSource lastObject];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [headView nightMode];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 88, 88)];
    [imageView sd_setImageWithURL:groupNextHeadModel.icon[@"url"]];
    [imageView nightMode];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(imageView.right + 10, imageView.origin.y + 5, self.view.width - imageView.width, 10)];
    title.text = groupNextHeadModel.name;
    [title nightMode];
    
    UILabel *bio = [[UILabel alloc] initWithFrame:CGRectMake(title.origin.x, title.bottom, self.view.width - imageView.width, 50)];
    bio.font = [UIFont systemFontOfSize:8];
    bio.textColor = [UIColor grayColor];
    bio.numberOfLines = 0;
    bio.text = groupNextHeadModel.bio;
    [bio nightMode];
    
    UILabel *mnum = [[UILabel alloc] initWithFrame:CGRectMake(bio.origin.x, 80, bio.width, 10)];
    mnum.text = [NSString stringWithFormat:@"%@ %@", groupNextHeadModel.mnum, @"成员"];
    mnum.font = [UIFont systemFontOfSize:10];
    mnum.textColor = [UIColor grayColor];
    [mnum nightMode];
    
    [headView addSubview:mnum];
    [headView addSubview:imageView];
    [imageView release];
    [headView addSubview:title];
    [title release];
    [headView addSubview:bio];
    [bio release];
    [mnum release];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 290;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark - tableview点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YXYGroupNextViewModel *groupNextViewModel = _viewArray[indexPath.row];
    [YXYNetWorkTool getWithUrl:groupNextViewModel.next parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSString *url = result[@"info"][@"sbj"][@"album"][@"next"];
        YXYSelectionViewController *selectionVC = [[YXYSelectionViewController alloc] initWithURL:url];
        [self.navigationController pushViewController:selectionVC animated:YES];
        [selectionVC release];
        
    } fail:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
